#include "workwindow.h"
#include "ui_workwindow.h"
//#include "workwindowModel.h"
#include <QtSql>
#include <iostream>


MainWindo::MainWindo(QWidget *parent, int us, bool bo) :
    QMainWindow(parent),
    ui(new Ui::MainWindo)
{
    ui->setupUi(this);
    this->UserId = us;
    this->RealUser = us;
    this->boss_flag = bo;
    MyModel *model = new MyModel(0,us, bo);
    this->model_table = model;
    model->user_id = UserId;
    std::cout << UserId<< "xxxx" <<std::endl;
    QString fio="";
    QSqlQuery sql = QSqlQuery();
    sql.prepare("Select s.fio_stuff from stuff s where s.id_stuff = ?");
    sql.addBindValue(this->UserId);
    sql.exec();
    if ( sql.isActive() ) {

        while ( sql.next() ) {
            fio = sql.value(0).toString();
        }
    }
   ui->mainLabel->setText(fio);
    this->sub_widget = new SubTasks();
    this->staff_widget = new StaffWin(0,us,fio);
    //model->boss = false;
    ui->tableView->setModel(model);
     ui->tableView->resizeColumnsToContents();
     ui->tableView->resizeRowsToContents();
     if (!this->boss_flag)
     {
         ui->label->hide();
         ui->label_2->hide();
         ui->label_3->hide();
         ui->label_4->hide();
         ui->label_5->hide();
         ui->label_6->hide();
         ui->addTask->hide();
         ui->newDeadLine->hide();
         ui->newDop->hide();
         ui->newPriority->hide();
         ui->newResourse->hide();
         ui->newStart->hide();
         ui->newTask->hide();
         ui->delTask->hide();
         ui->back_win->hide();

     }

    MyDEDelegate *ded = new MyDEDelegate(
            true, this);
    ui->tableView->setItemDelegateForColumn(2, ded);
    ui->tableView->setItemDelegateForColumn(4, ded);

}

void MainWindo::refresh_window(int user, bool boss)
{
    this->UserId = user;
    this->model_table->user_id = user;
    if (boss)
    {
        this->model_table->boss = true;
        ui->label->show();
        ui->label_2->show();
        ui->label_3->show();
        ui->label_4->show();
        ui->label_5->show();
        ui->label_6->show();
        ui->addTask->show();
        ui->newDeadLine->show();
        ui->newDop->show();
        ui->newPriority->show();
        ui->newResourse->show();
        ui->newStart->show();
        ui->newTask->show();
        ui->delTask->show();
        ui->back_win->show();
        ui->newStart->setDate(QDate::currentDate());
        ui->newDeadLine->setDate(QDate::currentDate());
    }
    else
    {
        this->model_table->boss = false;
        ui->label->hide();
        ui->label_2->hide();
        ui->label_3->hide();
        ui->label_4->hide();
        ui->label_5->hide();
        ui->label_6->hide();
        ui->addTask->hide();
        ui->newDeadLine->hide();
        ui->newDop->hide();
        ui->newPriority->hide();
        ui->newResourse->hide();
        ui->newStart->hide();
        ui->newTask->hide();
        ui->delTask->hide();
        ui->back_win->hide();
    }
    QSqlQuery sql = QSqlQuery();
    QString fio="";
    sql.prepare("Select s.fio_stuff from stuff s where s.id_stuff = ?");
    sql.addBindValue(this->UserId);
    sql.exec();
    if ( sql.isActive() ) {

        while ( sql.next() ) {
            fio = sql.value(0).toString();
        }
    }
   ui->mainLabel->setText(fio);
   this->model_table->refresh();
   ui->tableView->resizeRowsToContents();
}

MainWindo::~MainWindo()
{
    delete ui;
}


void MainWindo::on_pushButton_clicked()
{
    QString a = ui->tableView->model()->data(ui->tableView->model()->index(ui->tableView->currentIndex().row(),0)).toString();
    QString b = ui->tableView->model()->data(ui->tableView->model()->index(ui->tableView->currentIndex().row(),1)).toString();
    a = a.right(a.length()-1);
    ui->textEdit->append(a);
    this->sub_widget->task_num = a.toInt();
    this->sub_widget->task_name = b;
    this->sub_widget->update_win();
    this->sub_widget->show();

}

void MainWindo::on_pushButton_2_clicked()
{
   this->model_table->refresh();
    ui->tableView->resizeRowsToContents();
}

void MainWindo::on_delTask_clicked()
{
 QString a = ui->tableView->model()->data(ui->tableView->model()->index(ui->tableView->currentIndex().row(),0)).toString();
 a = a.right(a.length()-1);
 //int b= a.toInt();
 QSqlQuery sql = QSqlQuery();
 sql.prepare("DELETE FROM tasks t WHERE t.id_task = ?");
 sql.addBindValue(a);
 sql.exec();
 this->model_table->refresh();
 ui->tableView->resizeRowsToContents();

}

void MainWindo::on_staff_b_clicked()
{
    this->staff_widget->show();
}

void MainWindo::on_old_tasks_clicked()
{
    if (this->model_table->old)
        this->model_table->old = false;
    else
         this->model_table->old = true;
    this->model_table->refresh();
     ui->tableView->resizeRowsToContents();
}


void MainWindo::on_back_win_clicked()
{
    refresh_window(this->RealUser, false);
    return;
}

void MainWindo::on_addTask_clicked()
{
    QSqlQuery sql = QSqlQuery();
    sql.prepare("insert into tasks (id_staff, task_name, time_start, "
                "dead_line, priority, resources, description) "
                "VALUES (?,?,?,?,?,?,?)");
    sql.addBindValue(this->UserId);
    sql.addBindValue(ui->newTask->text());
    sql.addBindValue(ui->newStart->date());
    sql.addBindValue(ui->newDeadLine->date());
    sql.addBindValue(ui->newPriority->text());
    sql.addBindValue(ui->newResourse->text());
    sql.addBindValue(ui->newDop->text());
    sql.exec();

    this->model_table->refresh();
    ui->tableView->resizeRowsToContents();
}

void MainWindo::on_pushButton_3_clicked()
{
    refresh_window(1, true);
    return;
}
